Question 1: answer the question

Describe a problem or question in your field that lends itself to spatial analysis. An interesting problem in meta-science is the effect of physical location on scientific collaboration. More specifically, are researchers more likely to collaborate with researchers who are physically close? I.e. are researchers at Aarhus University more likely to collaborate with SDU than Boston University?

This is a task where spatial analysis would intersect nicely with network analysis. One could use spatial analysis for the visualization. In addition, spatial analysis could help highlight additional important factors like administrative- and country boundaries as well as the importance of traffic networks.

Question 2: Answer the question

List data layers that you think are necessary to answer your question/solve your problem. Describe examples of two or three of your listed layers and see if you can find them on the internet.

The following layers are necessary to answer the question: A map of administrative borders; A map of transport / roads; locations of the universities; and co-authorship connections between the universities.

The first two layers (constituting the map) can be found using the Esri.WorldStreetMap view. This provides both administrative border as well as transport networks. Locations of universities can be found using the OpenStreetMap API and choosing the administrative buildings (as not to differentiate between faculties within a university). This would conceivably involve a bit of string-parsing but nothing too complex. Finally, co-authorship can be found by scraping the metadata of ArXiv papers (or whatever available source) and creating an undirected network.

Question 3: Make a Map (option 2)

You wish to travel to Chicago for a study stay but wish to stay away from the most crime-ridden areas. You have a friend at Evanston, who invited you in. Is it safe to stay at her place? Make a map that allows both of you to explore the local situation. Use the ChicagoCrime.csv (attached here or on Github) to create an interactive map of murders in the city. Is a heatmap or clustered points better?

Let’s find out!

3.1

Create a standalone .html map in Leaflet showing at least basic topography and relief, and load in the table of points. Make sure she can see the locations of crime and crime attributes when you hovers over the point markers.

# Loading packages
pacman::p_load(tidyverse, leaflet, htmltools, lubridate)

raw_crimedf <- read_csv('data/ChicagoCrimes2017.csv')
## Rows: 268505 Columns: 22
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (12): Case Number, Date, Block, IUCR, Primary Type, Description, Locatio...
## dbl  (8): ID, Ward, Community Area, X Coordinate, Y Coordinate, Year, Latitu...
## lgl  (2): Arrest, Domestic
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Types of crimes we care about (to minimize processing) with respective seriousness scores (my judgement)
danger_crimes <- c("ASSAULT", "HOMICIDE", "CRIM SEXUAL ASSAULT", "ROBBERY")
loc_crimedf <- raw_crimedf %>%
  filter(!Domestic & (`Primary Type` %in% danger_crimes)) %>%  #  Remove domestic crimes as these aren't relevant for visitors
  select(Date, `Primary Type`, `Location Description`, Longitude, Latitude) %>% 
  drop_na(Longitude, Latitude) %>% 
  mutate(Date = parse_date_time(Date, "mdy HMS"))


chicago_map <- leaflet() %>% 
  addTiles() %>% 
  addAwesomeMarkers(lng = loc_crimedf$Longitude, 
             lat = loc_crimedf$Latitude,
             popup = paste('Crime:', loc_crimedf$`Primary Type`, '<br>',
                           'Location:', loc_crimedf$`Location Description`),
             clusterOptions = markerClusterOptions())

chicago_map

I had to add the clusterOptions or the map simply couldn’t be processed. But

3.2

Consider adding elements such as minimap() and measure() for easier map interaction I’ll add a minimap :))

chicago_map %>% 
  addMiniMap(toggleDisplay = TRUE,
             position = "bottomright") 

3.3

Can you create a heatmap of crime? Let’s give it a go!

pacman::p_load(leaflet.extras)

loc_crimedf <- loc_crimedf %>% 
  mutate(seriousness = case_when(
    `Primary Type` == 'ASSAULT' ~ 3,
    `Primary Type` == 'ROBBERY' ~ 2,
    `Primary Type` == 'HOMICIDE' ~ 5,
    `Primary Type` == 'CRIM SEXUAL ASSAULT' ~ 4,
  ))

loc_crimedf
## # A tibble: 27,958 x 6
##    Date                `Primary Type`      `Location Descrip~ Longitude Latitude
##    <dttm>              <chr>               <chr>                  <dbl>    <dbl>
##  1 2017-11-25 10:00:00 CRIM SEXUAL ASSAULT ALLEY                  -87.7     41.7
##  2 2017-02-17 12:20:00 ROBBERY             CONVENIENCE STORE      -87.7     41.9
##  3 2017-10-14 07:20:00 CRIM SEXUAL ASSAULT STREET                 -87.6     41.7
##  4 2017-04-23 06:30:00 ROBBERY             STREET                 -87.7     41.8
##  5 2017-03-03 01:45:00 HOMICIDE            ALLEY                  -87.6     41.8
##  6 2017-12-21 09:15:00 ROBBERY             STREET                 -87.7     41.9
##  7 2017-12-07 05:35:00 ROBBERY             CONVENIENCE STORE      -87.7     42.0
##  8 2017-12-17 03:30:00 ROBBERY             STREET                 -87.8     41.9
##  9 2017-12-01 06:00:00 CRIM SEXUAL ASSAULT VEHICLE NON-COMME~     -87.6     41.8
## 10 2017-06-18 01:19:00 HOMICIDE            STREET                 -87.7     41.9
## # ... with 27,948 more rows, and 1 more variable: seriousness <dbl>
leaflet() %>% 
  addTiles() %>% 
  addHeatmap(lng = loc_crimedf$Longitude,
             lat = loc_crimedf$Latitude, 
             intensity = loc_crimedf$seriousness,
             max = 400,
             radius = 10)

It has taken a bit of parameter tuning to get the heatmap to be meaningful. It also greatly depends on the zoom level with higher scales making it less useful.

3.4

Explore differentiating the markers (e.g. by color for different kinds of crime)

# Function to get the color of a specific crime. Returns a vector of colors
getColor <- function(crime_types) {
  case_when(
    crime_types == "HOMICIDE" ~ "darkgray",
    crime_types == "ASSAULT" ~ "red",
    crime_types == "CRIM SEXUAL ASSAULT" ~ "pink",
    crime_types == "ROBBERY" ~ "orange",
  )
}

# Creates a list of icons for plotting
icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(loc_crimedf$`Primary Type`)
)


chicago_map_new <- leaflet() %>% 
  addTiles() %>% 
  addAwesomeMarkers(lng = loc_crimedf$Longitude, 
             lat = loc_crimedf$Latitude,
             popup = paste('Crime:', loc_crimedf$`Primary Type`, '<br>',
                           'Location:', loc_crimedf$`Location Description`),
             icon = icons,
             clusterOptions = markerClusterOptions())

chicago_map_new

3.5

Explore the option of clustering markers with addMarkers(clusterOptions = markerClusterOptions()). Do you recommend marker clustering here? Due to the immense processing time, I have already done this in 3.1. However, this is because I definitely recommend marker clustering - otherwise it becomes a jumbled mess. It also encourages exploration, as you can more easily decide your level of granularity.